<?php require("sessionStart.php");

/*---Function Begins---*/

//Determines if anything has been entered within Username and password. It does so by
//determining if anything is present in the associative array that by default contains
//the contents of $_GET or $_POST. It also checks if the associative array has the key
//'Username' which is the name value of the text box marked further below
if ( (sizeof ($_REQUEST) > 0) && isset($_REQUEST['Username']))
{
      //Strips whitespace from beginning and end of entered username and assigns that value to
      //the variable $username.
	$username = trim($_REQUEST['Username']);
      //Sets the value of entered password to the variable $pass
	$pass = $_REQUEST['Password'];


         //Checks to see if both username and password are entered with no value.
           if (( $_REQUEST["Username"] != "")  && ($_REQUEST["Password"] != "") )
           {



					  //Open set to read the contents of the document containing the usernames and password
					  //information. If an error occurs, it will quit the opening process and display the
					  //profanity...
				$file = fopen("passwords.txt","r") or exit("Shit happens!");

						//tests for end-of-file the file pointer
				while (!feof($file))
				{
							  //Reads one line from $file (passwords.txt) and sets it to the variable $line
					$line = fgets($file);

							  //compares the entered username ($username, which came from $_REQUEST['Username'])
							  //to the strings returned by the string tokenizer which split the strings
							  //according to the character ","
					if ( $username == strtok($line,"|") )
					   {

									  //This first calls the convert function provided by halojoy. alexvartani
									  //is the key word to decrypt the text... Don't tell anyone. It checks
									  //if the password matches any password on the list (passwords.txt)
						if ( $pass ==  convert(strtok("|"),"alexvartani") )
						   {

											  //sets $group to string located immediately after the
											  //comfirmed password. This string holds the group value.
									$group =  strtok("|");



											  //Remove whitespace and make $username the current session's
											  //username ($_SESSION['Username'])
										$_SESSION['Username'] = trim($username);



									  //Remove whitespace and make $permission the current session's
									  //permission level ($_SESSION['group'])
										$_SESSION['group'] = trim($group);



						   }
					  }
				}

						//Close passwords.txt
				fclose($file);
	}
}
/*---Function End---*/


header( 'Location:' . $_SERVER["HTTP_REFERER"]  ) ;



// String EnCrypt + DeCrypt function
// Author: halojoy, July 2006
// link: http://www.phpbuilder.com/board/showthread.php?t=10326721
function convert($str,$ky='')
	{
		if($ky=='')return $str;
		$ky=str_replace(chr(32),'',$ky);
		if(strlen($ky)<8)exit('key error');
		$kl=strlen($ky)<32?strlen($ky):32;
		$k=array();for($i=0;$i<$kl;$i++){
		$k[$i]=ord($ky{$i})&0x1F;}
		$j=0;for($i=0;$i<strlen($str);$i++){
		$e=ord($str{$i});
		$str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
		$j++;$j=$j==$kl?0:$j;}
		return $str;
	}
	///////////////////////////////////

	// Secret key to encrypt/decrypt with
	$key='alexvartani'; // 8-32 characters without spaces

	// String to encrypt
	$string1='To be or not to be, that is the question';

	// EnCrypt string
	$string2=convert($string1,$key);

	// DeCrypt back
	$string3=convert($string2,$key);



?>
